local AddPlayerPostInit = AddPlayerPostInit
GLOBAL.setfenv(1, GLOBAL)

local function HasLonelyHeart(inst)
    if not inst.components.inventory or not inst.components.sanity then
        return
    end

    if inst.components.inventory:Has("lonely_heart", 1) then
        inst.components.sanity.externalmodifiers:SetModifier(inst, 2, "lonely_heart")
    else
        inst.components.sanity.externalmodifiers:RemoveModifier(inst, "lonely_heart")
    end
end

AddPlayerPostInit(function(inst)
    if not TheWorld.ismastersim then
        return
    end

    inst:DoPeriodicTask(FRAMES, HasLonelyHeart)
end)
